package view.WelcomePanels.SubPanels.FormPanels;

import controller.AccountController;
import dto.WelcomeDTO;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/**
 *
 * @author Joachim
 */
public abstract class AbstractFormPanel extends JPanel {
      
    protected WelcomeDTO dto;
    private AccountController controller;
    protected GridBagConstraints c;
    
    private JTextField txtEmail;
    
    public AbstractFormPanel(WelcomeDTO dto, AccountController controller) {
        this.dto = dto;
        this.controller = controller;
        this.c = new GridBagConstraints();
        initPanel();
        addBasicComponents();
        addSpecificComponents();
    }
    
    private void initPanel() {
        this.setOpaque(false);
        this.setLayout(new GridBagLayout());
    }
    
    private void addBasicComponents() {
        c.insets = new Insets(150,0,0,0);
        c.anchor = GridBagConstraints.PAGE_END;
        {
            JLabel lblEmail = new JLabel("E-mail: ");
            c.gridx = 0;
            c.gridy = 0;
            c.weightx = 0.1;
            c.fill = GridBagConstraints.BOTH;            
            this.add(lblEmail,c);
        }
        {
            txtEmail = new JTextField(15);
            txtEmail.setText(controller.getEmail());
            dto.setEmail(txtEmail.getText());
            txtEmail.setBorder(BorderFactory.createLineBorder(new Color(28,28,28,100)));
            txtEmail.getDocument().addDocumentListener(new DocumentListener() {
                public void changedUpdate(DocumentEvent e) {fillDTO();}
                public void removeUpdate(DocumentEvent e) {fillDTO();}
                public void insertUpdate(DocumentEvent e) {fillDTO();}
                public void fillDTO() {
                   dto.setEmail(txtEmail.getText());
                }
            });
            c.gridx = 1;
            c.gridy = 0;
            c.weightx = 1.0;
            c.fill = GridBagConstraints.BOTH;
            this.add(txtEmail,c);
        }
    }
    
    protected abstract void addSpecificComponents();
     
}
